<?php

namespace App\Console\Commands;

use App\Imports\MultipleImport;
use App\Models\JobType;
use App\Models\ModelNumber;
use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\Specification;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class AddJobTypePrice extends Command
{
    /**
     * 导入工种价格
     * The name and signature of the console command.
     * php artisan command:addjobtypeprice
     * @var string
     */
    protected $signature = 'command:addjobtypeprice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $file = public_path('excel').'/工序工价.xlsx';//获取文件路径
        $import = new MultipleImport(3);
        Excel::import($import, $file);
        $results = $import->sheet[0]->data->toArray();
        //$results2 = $import->sheet[1]->data->toArray();
        //$results3 = $import->sheet[2]->data->toArray();
        //dd($results,$results2,$results3);
        unset($results[0]);
        unset($results[1]);
        $results_s = $results;
        foreach($results as $k=>$v){

            if(empty($v[1])){
                //p($results_s[$k-1][1]);
                $results_s[$k][1] =  $results_s[$k-1][1];
            }
            if(empty($v[2])){
                $results_s[$k][2] =  $results_s[$k-1][2];
            }
            if(empty($v[3])){
                $results_s[$k][3] =  $results_s[$k-1][3];
            }
            if(empty($v[5]) && empty($v[4])){
                continue;
            }
        }
        //p($results_s);
        foreach($results_s as $k=>$v){
            $this->info('导入：产品名称='.$v[1].";型号=".$v[2].";规格=".$v[3].";工种=".$v[4].";工种价格=".$v[5]);
            if(empty($v[5])){
                continue;
            }
            if(empty($v[4])){
                continue;
            }
            if(empty($v[3])){
                continue;
            }
            if(empty($v[2])){
                continue;
            }
            if(empty($v[1])){
                continue;
            }
            $product = Product::where('product_name',$v[1])->first();
            $model_number = ModelNumber::updateOrCreate(['model_number'=>$v[2]],['model_number'=>$v[2]]);
            $job_type = JobType::updateOrCreate(['job_type'=>$v[4]],['job_type'=>$v[4]]);
            $arr = [];
            if(strpos($v[3],"\n")){
                $arr = explode("\n",$v[3]);
                //p($arr);
                foreach($arr as $kk=>$vv){
                    $specification = Specification::updateOrCreate(['specification'=>$vv],['specification'=>$vv]);
                    ModelNumberSpecification::updateOrCreate(
                        ['model_number_id'=>$model_number->id,'specification_id'=>$specification->id],
                        [
                            'model_number_id'=>$model_number->id,
                            'specification_id'=>$specification->id,
                            'product_id'=>$product->id
                        ]
                    );
                    $where = [
                        'product_id'=>$product->id,
                        'model_number_id'=>$model_number->id,
                        'specification_id'=>$specification->id,
                        'job_type_id'=>$job_type->id
                    ];
                    ProductCenter::updateOrCreate($where,[$where,'price'=>$v[5]]);
                }
            }else{
                $specification = Specification::updateOrCreate(['specification'=>$v[3]],['specification'=>$v[3]]);
                ModelNumberSpecification::updateOrCreate(
                    ['model_number_id'=>$model_number->id,'specification_id'=>$specification->id],
                    [
                        'model_number_id'=>$model_number->id,
                        'specification_id'=>$specification->id,
                        'product_id'=>$product->id
                    ]
                );
                $where = [
                    'product_id'=>$product->id,
                    'model_number_id'=>$model_number->id,
                    'specification_id'=>$specification->id,
                    'job_type_id'=>$job_type->id
                ];
                ProductCenter::updateOrCreate($where,[$where,'price'=>$v[5]]);
            }



        }
    }


}
